package com.elink.enginerring.news;

import java.util.Map;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.elink.enginerring.common.Constant;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.news.co.NewsCo;
import com.elink.enginerring.news.entity.News;
import com.elink.enginerring.news.service.INewsService;
import com.elink.enginerring.user.entity.User;
import com.elink.enginerring.util.IDGenerator;
import com.elink.enginerring.util.JSONUtil;

@Controller
@RequestMapping("/news")
public class NewsController {
	@Autowired
	private INewsService newsService;
	
	/**
	 * 获取新闻资讯列表
	 * @param co
	 * @return
	 */
	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getList(@ModelAttribute @Valid NewsCo co) {
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return newsService.getListOfPage(co);
	}

	/**
	 * 添加新闻
	 * @param news
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>add(@ModelAttribute @Valid News news,HttpSession session){
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		User user = (User) session.getAttribute(Constant.su_in_session);
		news.setUserId(user.getUserId());
		return JSONUtil.getJSON(ErrorType.SUCCESS,newsService.add(news));
	}
	/**
	 * 删除文件
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> del(@RequestParam("ids")String ids){
		DataSourceContextHolder.setDbType(DataSourceType.DBNEWS);
		return newsService.del(ids);
	}
}
